package com.stars.services.impl.system;/**
 * Created by luodajie on 2019/1/5.
 */

import com.stars.common.base.BaseCode;
import com.stars.common.base.BaseConstant;
import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.common.valid.MyException;
import com.stars.dao.mapper.system.SystemPermissionRelationMapper;
import com.stars.entity.sys.SystemPermissionRelation;
import com.stars.services.impl.BaseConfigServiceImpl;
import com.stars.services.system.PermissionRelationConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author :dj
 * @version : 1.0.0
 * @description
 * @Date 2019-01-05 12:17
 * @note
 */
@Service
public class PermissionRelationConfigServiceImpl extends BaseConfigServiceImpl<SystemPermissionRelationMapper, SystemPermissionRelation> implements  PermissionRelationConfigService {

    @Autowired
    private SystemPermissionRelationMapper systemPermissionRelationMapper;

    @Override
    public ResultMsg permissionRelationInsert(String ids,String roleId) throws Exception {
        String[] idArray = addPerList(ids, roleId);
        for (String id : idArray) {
            if(StringUtils.isNotBlank(id)){
                SystemPermissionRelation systemPermissionRelation = new SystemPermissionRelation();
                systemPermissionRelation.setPerId(id);
                systemPermissionRelation.setPerRoleId(roleId);
                SystemPermissionRelation byRelation = systemPermissionRelationMapper.findByRelation(systemPermissionRelation);
                if (null != byRelation) {
                    continue;
                }
                systemPermissionRelationMapper.insertSelective(systemPermissionRelation);
            }
        }
        return ResultMsg.success();
    }

    private String [] addPerList(String ids,String roleId){
        if(StringUtils.isBlank(ids)){
            throw  new MyException("没有添加菜单资源");
        }
        if(StringUtils.isBlank(roleId)){
            throw  new MyException("没有选择角色");
        }
        return ids.split(BaseConstant.SPOT);
    }
    @Override
    public ResultMsg updateEntity(String ids,String roleId) throws Exception {
        String[] idArray = addPerList(ids, roleId);
        for (String id : idArray) {
            SystemPermissionRelation systemPermissionRelation = new SystemPermissionRelation();
            systemPermissionRelation.setPerId(id);
            systemPermissionRelation.setPerRoleId(roleId);
            systemPermissionRelationMapper.deleteByRoleIdAndPerId(systemPermissionRelation);
        }
        return ResultMsg.success();
    }

    @Transactional
    @Override
    public ResultMsg updateByRelation(Map<String, Object> map) throws Exception {

        System.out.println(map.get("role"));

        List roles = (List) map.get("role");

        for (int i = 0; i < roles.size(); i++) {
            Map<String, Object> each = (Map<String, Object>) roles.get(i);

            String per_role_id = each.get("per_role_id") + "";
            String per_id = each.get("per_id") + "";

            SystemPermissionRelation systemPermissionRelation = new SystemPermissionRelation();
            systemPermissionRelation.setPerId(per_id);
            systemPermissionRelation.setPerRoleId(per_role_id);

            if (1 == Integer.parseInt(each.get("flag") + "")) {
                systemPermissionRelationMapper.insertSelective(systemPermissionRelation);
            }

            if (0 == Integer.parseInt(each.get("flag") + "")) {
                systemPermissionRelationMapper.deleteByRoleIdAndPerId(systemPermissionRelation);
            }
        }
        return ResultMsg.success();
    }
}
